package worst.shopping.adapter;

import worst.shopping.adapter.ImageCursorAdapter;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;

public abstract class AHighlightableImageCursorAdapter extends ImageCursorAdapter {

	private Drawable _highlight;
	private String _listName;
	
	
	public AHighlightableImageCursorAdapter(Context context, int layout,
			Cursor cursor, String[] from, int[] to, Drawable highlight, String listname) {
		super(context, layout, cursor, from, to);
		
		_highlight = highlight;
		_listName = listname;
	}
	
	@Override
	public View getView(int pos, View inView, ViewGroup parent) {
		inView = super.getView(pos, inView, parent);	
		
		ViewHolder viewHolder = (ViewHolder) inView.getTag();
		
		//not that smart ... ? 
		//cursor should be at position pos already

		boolean itemIsSelected = isSelected(_listName, viewHolder.productName.getText().toString(), pos);
		
		if(itemIsSelected)
		{
			inView.setBackgroundDrawable(_highlight);
			
			viewHolder.productName.setTextColor(Color.BLACK);
			viewHolder.productDescription.setTextColor(Color.BLACK);
			viewHolder.productCount.setTextColor(Color.BLACK);
			viewHolder.productPriceTotal.setTextColor(Color.BLACK);
		}
		else
		{
			viewHolder.productName.setTextColor(Color.WHITE);
			viewHolder.productDescription.setTextColor(Color.GRAY);	
			viewHolder.productCount.setTextColor(Color.WHITE);
			viewHolder.productPriceTotal.setTextColor(Color.WHITE);
			inView.setBackgroundColor(Color.TRANSPARENT);
		}
		
		return inView;
	}
	
	public String getListName()
	{
		return _listName;
	}
	
	public void setListName(String listname)
	{
		_listName = listname;
	}
	
	protected abstract boolean isSelected(String listname, String productname, int cursorPosition);
}
